// pages/mine/auth/index.js
const app = getApp();
var serverUrl = app.globalData.serverUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onecard: "/asset/img/uploads.png",
    twocard: "/asset/img/uploads.png",
    hideOne:"",
    hideTwo:"hide",
    name:"",
    sex:"",
    nation:"",
    brith:"",
    address:"",
    card:"",
    term:"",
    lssue:"",
    disbtn:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userInfo = wx.getStorageSync("userInfo");
    if(!userInfo.uid){
      wx.redirectTo({
        url: '../login/index',
      })
      if( userInfo.is_auth == 1 ){
        wx.request({
          url: serverUrl +'api/v1/users/authies/uid/'+userInfo.uid,
          method: "GET",
          dataType: "json",
          success:function(data){
            data = data.data;
            if(data.code == 200){
              that.setData({
                onecard: data.data.card_image_just,
                hideTwo: "",
                twocard: data.data.card_image_back,
                name: data.data.realname,
                sex: data.data.sex,
                nation: data.data.nation,
                brith: data.data.brith,
                address: data.data.address,
                card: data.data.id_card,
                term: data.data.term,
                lssue: data.data.lssue
              })
            } else {
              that.setData({
                disbtn: true
              })
              wx.showToast({
                title: '【'+data.message+'】!请重新进入后重试',
                icon: "none",
                duration: 2000
              })
            }
          },
          error:function(data){
            that.setData({
              disbtn: true
            })
            wx.showToast({
              title: '服务器错误!请重新进入后重试',
              icon: "none",
              duration: 2000
            })
          }
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**第一张身份证上传**/
  oneuploads:function(){
    var that = this;
    if (that.data.onecard != "/asset/img/uploads.png"){
      return false;
    }
    wx.chooseImage({
      count:1,
      success:function(res){
        console.log(res);
        let tempFilePaths = res.tempFilePaths;
        that.setData({
          onecard:tempFilePaths[0],
          hideTwo:""
        })
      },
      error:function(res){
        console.log(res);
      }
    })
  },

  /**第二张身份证上传**/
  twouploads: function () {
    var that = this;
    if (that.data.twocard != "/asset/img/uploads.png") {
      return false;
    }
    wx.chooseImage({
      count: 1,
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
        that.setData({
          twocard: tempFilePaths[0],
        })
      }
    })
  },

  /**首页跳转**/
  returnIndex: function () {
    wx.switchTab({
      url: '../../index/index',
    })
  }
})